<template>
  <div class="box">
    <!-- 1.0 登录界面的绘制 -->
    <div class="login-inner">
      <h1>SB外卖系统</h1>
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        class="demo-ruleForm"
        size="mini"
      >
        <el-form-item prop="age">
          <el-input v-model="ruleForm.uname" prefix-icon="iconfont icon-yonghu">
          </el-input>
        </el-form-item>
        <el-form-item prop="pass">
          <el-input
            :type="hiddenPass ? 'password' : 'text'"
            v-model="ruleForm.pass"
            prefix-icon="iconfont icon-mima"
            autocomplete="off"
          >
            <template #suffix>
              <i
                class="iconfont icon-eyes"
                @click="hiddenPass = !hiddenPass"
              ></i>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    var checkAge = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("账号不符合标准"));
      }
      setTimeout(() => {
        if (!Number.isInteger(value)) {
          callback(new Error("请输入数字值"));
        } else {
          if (value < 18) {
            callback(new Error("必须年满18岁"));
          } else {
            callback();
          }
        }
      }, 1000);
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        pass: "",
        checkPass: "",
        age: ""
      },
      rules: {
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
        age: [{ validator: checkAge, trigger: "blur" }]
      },
      //显示隐藏密码
      hiddenPass: true
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          alert("submit!");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>

<style lang="less" scoped>
.box {
  height: 100%;
  background-color: #2d3a4b;
  display: flex;
  justify-content: center;
  align-items: center;

  .login-inner {
    width: 340px;
    height: 240px;

    h1 {
      text-align: center;
      color: #fff;
      font-size: 35px;
      letter-spacing: 1.5px;
      margin-bottom: 30px;
    }
    .el-button {
      width: 100%;
    }
  }
}
</style>
